﻿#region Header
// Copyright 2007-2011, Jerónimo Milea
// 
// This file is part of NetJsWire.
// 
//     NetJsWire is free software: you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation, either version 3 of the License, or
//     (at your option) any later version.
// 
//     NetJsWire is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.
// 
//     You should have received a copy of the GNU General Public License
//     along with NetJsWire.  If not, see <http://www.gnu.org/licenses/>.
#endregion

using System.Windows.Forms;
using System.Windows.Forms.Layout;
using NetJsWire.Util;
using NetJsWire.Widgets.Ui.Layout;

namespace NetJsWire.Widgets.Ui.Form
{
    public class FormLayoutEngine : LayoutEngine
    {
        public override bool Layout(
            object container,
            LayoutEventArgs layoutEventArgs )
        {
            var parent = container as FormPanel;
            // Use DisplayRectangle so that parent.Padding is honored.
            var parentDisplayRectangle = parent.DisplayRectangle;
            var nextControlLocation = parentDisplayRectangle.Location;

            var nextButtonLocation = parentDisplayRectangle.Location;

            if ( parent.ButtonAlign == ButtonAlign.Right )
            {
                nextButtonLocation.Offset( ( parent.Width - 20 ), 0 );
            }

            if ( !string.IsNullOrEmpty( parent.Title ) )
            {
                nextControlLocation.Offset( 0, 23 );
            }

            if ( !( parent.Layout == LayoutType.Form ) )
            {
                foreach ( Control c in parent.Controls )
                {
                }
                return true;
            }

            foreach ( Control c in parent.Controls )
            {
                // Only apply layout to visible controls.
                if ( !c.Visible )
                {
                    continue;
                }

                if ( c is Button )
                {
                    //if ( parent.ButtonAlign == ButtonAlign.Left )
                    //{
                    //    nextButtonLocation.Y = parent.Height - c.Height - c.Margin.Top;
                    //    nextButtonLocation.Offset( c.Margin.Left, -c.Margin.Bottom );
                    //}

                    //c.Location = nextButtonLocation;
                    //nextButtonLocation.X += c.Size.Width + c.Margin.Right;
                    //nextButtonLocation.Y = parent.Location.Y + parent.Height - c.Height - c.Margin.Top;

                    if ( c is Button && ( c as Button ).AutoPositioned )
                    {
                        nextButtonLocation.Y = parent.Height - c.Height - c.Margin.Top - parent.Padding.Top -
                                               parent.Padding.Bottom;

                        if ( parent.ButtonAlign == ButtonAlign.Left )
                        {
                            nextButtonLocation.Offset( c.Margin.Left, -c.Margin.Bottom );
                            c.Location = nextButtonLocation;

                            nextButtonLocation.X += c.Size.Width + c.Margin.Right;
                        }
                        else if ( parent.ButtonAlign == ButtonAlign.Right )
                        {
                            nextButtonLocation.Offset( -( c.Width + c.Margin.Left + c.Margin.Right ), 0 );
                            c.Location = nextButtonLocation;
                        }
                    }
                }
                else
                {
                    // Respect the margin of the control:
                    // shift over the left and the top.
                    //if ( c is Ui.Form.FieldSet )
                    //    nextControlLocation.Offset( c.Margin.Left, c.Margin.Top );
                    //else
                    //    nextControlLocation.Offset(
                    //        c.Margin.Left + ( parent.LabelAlign == LabelAlign.Left ? parent.LabelWidth : 0 ) + 5,
                    //        c.Margin.Top );

                    //// Set the location of the control.
                    //c.Location = nextControlLocation;

                    //// Set the autosized controls to their 
                    //// autosized heights.
                    //if ( c.AutoSize )
                    //{
                    //    if ( c is Ui.Form.FieldSet )
                    //    {
                    //        //c.Size = new Size( parent.Width - parent.Padding.Left - parent.Padding.Right - 25, c.Height );
                    //    }
                    //    else
                    //    {
                    //        //c.Size = new Size( parent.Width - parent.Padding.Left - parent.Padding.Right - ( parent.LabelWidth + 25 ), c.Height );
                    //    }

                    //    //c.Size = c.GetPreferredSize( parentDisplayRectangle.Size );
                    //}

                    //// Move X back to the display rectangle origin.
                    //nextControlLocation.X = parentDisplayRectangle.X;

                    //// Increment Y by the height of the control 
                    //// and the bottom margin.
                    //nextControlLocation.Y += c.Height + c.Margin.Bottom;
                }
            }

            // Optional: Return whether or not the container's 
            // parent should perform layout as a result of this 
            // layout. Some layout engines return the value of 
            // the container's AutoSize property.

            return false;
        }
    }
}